local BattleStoryController = class("BattleStoryController",function()
	return display.newNode()
end)

--done
function BattleStoryController.createByFormationID(formationID)
	if formationID then
		local scriptName = "scripts.battle_storys.formation_"..formationID
		local ret = app:loadResScript(scriptName)
		if ret then
        	local battleStoryController = BattleStoryController.new()
			battleStoryController.script = ret
			--print("====>",scriptName,"loaded")
			return battleStoryController
		end
	end
	return nil
end

--done
function BattleStoryController:ctor()
	self.script = nil
	self.tasks = {}
end

--done
function BattleStoryController:init(battleCtl)
	self.battleCtl = battleCtl
	local function GuideEndListener(event)
		self:onGuideFinished(event)
	end
	app:addCustomEventListener("EVENT_GUIDE_END",GuideEndListener,self)
end

-- function BattleStoryController:onSelectEntity(event)
-- 	local selTargetId = event.targetId
-- 	for name,task in pairs(self.tasks) do
-- 		local targetId = task.targetId
-- 		if targetId and targetId==selTargetId then
-- 			task.targetId=nil
-- 		end
-- 	end
-- end

function BattleStoryController:onGuideFinished(event)
	-- local endFeatureId = event.featureId
	for name,task in pairs(self.tasks) do
		local featureId = task.featureId
		if featureId then
			task.featureId=nil
		end
	end
end

--done
function BattleStoryController:checkIsVaild(event)
	return self.script ~= nil and type(self.script[event])=="function"
end

--done
function BattleStoryController:execTask(name,...)
	local task = self.script[name]
	local co = coroutine.create(task)
	self.tasks[name] = {co=co,wait=nil,params={...}}
	--print("execTask ",name)
end

--done
function BattleStoryController:waitSeconds(s)
	coroutine.yield("wait",s)
end

function BattleStoryController:waitTargetSelect(id)
	coroutine.yield("target_select",id)
end

--done
function BattleStoryController:tick(t)
	local script = self.script
	for name,task in pairs(self.tasks) do
		if task then
			local co = task.co
			local wait = task.wait
			local params = task.params
			if wait and wait > 0 then
				task.wait = wait - t
			elseif task.targetId then
			elseif task.featureId then
			elseif coroutine.status(co)=="suspended" then
				--print("task",name,"wait ",wait,"params:",unpack(params))
				local ret , yieldName,yieldValue = coroutine.resume(co,script,unpack(params))
				--print("task",name,"resume",ret,yieldName,yieldValue)
				if ret then
					if yieldName == "wait" then
						task.wait = yieldValue
					elseif yieldName == "target_select" then
						task.targetId = yieldValue
					elseif yieldName == "guide" then
						task.featureId = yieldValue
					end
				end
			elseif coroutine.status(co)=="dead" then
				--print("task ",name,"dead")
				self.tasks[name] = nil
			else
				assert(false,"coroutine error wait:",wait,"status:",coroutine.status(co))
			end
		end
	end
end

--done
function BattleStoryController:getEntityViews()
	return self.battleCtl:getEntityViews()
end

--done
function BattleStoryController:getEntityModelById(id)
	return self.battleCtl:getEntityModelById(id)
end

--done
function BattleStoryController:doTalkByID(entityId,talk)
	local view = self.battleCtl:getEntityView(entityId)
	local entity = self.battleCtl:getEntityModelById(view:getUnitModelId())
	local text = talk.text
	local duration = talk.duration + 0.5
	local showType = talk.showType or 0
	local showBubble = (showType == 1 or showType==0)
	local showChat = (showType == 2 or showType==0)
	if showBubble then
		view:appendBubbleTask({text=text,duration=duration})
	end
	-- if showChat then
	-- 	local name = entity:getName()
	-- 	app:dispatchEvent({name="AppendChatMessage",msg=text,title=name,channel=6})
	-- end
end

--done
function BattleStoryController:displayStancePanel(b)
	self.battleCtl:forceDisplayUI("stanceSwitchPanel",b)
end

function BattleStoryController:doGameOver()
	self.battleCtl:forceGameOver()
end

--done
function BattleStoryController:doGuideByFeatureId(featureId)
	if featureId then
		local uiRoot = self.battleCtl:getBattleUIRootNode()
		--print("-----------------doGuideByFeatureId---featureId:"..featureId)
		app:dispatchCustomEvent("EVENT_START_UI_GUIDE_CONDITION_BY_ID",{uiRoot = uiRoot,featureId = featureId})
		coroutine.yield("guide",featureId)
	end
end

--done
--设置战斗是否可以触摸
function BattleStoryController:doSetTouchEnable(b)
	self.battleCtl:setBattleUITouchEnabled(b)
end

--done
--指定战斗是否为自动战斗
function BattleStoryController:doSetBattleAuto(b)
	self.battleCtl:setBattleAuto(b)
end

--done
function BattleStoryController:doSetAutoBattleVisible(b)
	self.battleCtl:setAutoBattleVisible(b)
end

function BattleStoryController:setGameOverWin(isWin)
	self.isWin = isWin
end

-- function BattleStoryController:displaySkillCDPanel(b)
-- 	self.battleCtl:forceDisplayUI("skillCDPanel",b)
-- end

-- function BattleStoryController:displayInputProgressBar(b)
-- 	self.battleCtl:forceDisplayUI("inputProgressBar",b)
-- end

-- function BattleStoryController:enableInput(b)
-- 	self.battleCtl:enableInput(b)
-- end
--=======================events=================================
--done
function BattleStoryController:onStart()
	if not self:checkIsVaild("onStart") then
		return 
	end

	self:execTask("onStart",self)
end

--done
function BattleStoryController:onGameTime(time)
	-- if self:checkIsVaild("onGameTime") then
	-- 	self:execTask("onGameTime",self,time)
	-- end
	
	local eachTime = "onGameTime_"..time
	if self:checkIsVaild(eachTime) then
		self:execTask(eachTime,self)
		return true
	end
	return false
end

--暂时不需要该功能
-- function BattleStoryController:onBeginSkill(entity,view,skillId)
-- 	if not self:checkIsVaild("onBeginSkill") then
-- 		return 
-- 	end

-- 	self:execTask("onBeginSkill",self,entity,skillId)
-- end

--暂时不需要该功能
-- function BattleStoryController:onHurt(entity,view,skillId)
-- 	if not self:checkIsVaild("onHurt") then
-- 		return 
-- 	end

-- 	self:execTask("onHurt",self,entity,skillId)
-- end

--done
function BattleStoryController:onDead(entity,view)
	if not self:checkIsVaild("onDead") then
		return 
	end
	self:execTask("onDead",self,entity)
end

function BattleStoryController:onGameOver()
	-- if self.isWin ~= nil then
	-- 	self.battleCtl:playGameOverSound(self.isWin)
	-- end
end

return BattleStoryController